﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1.StudentTask.Model
{
    class Student : ICloneable, IComparable<Student>
    {
        public string FirstName { get; set; }
        public string MiddletName { get; set; }
        public string LastName { get; set; }

        public int SSN { get; set; }

        public string Address { get; set; }
        public string MobilePhone { get; set; }
        public string Email { get; set; }

        public int Course { get; set; }

        public University University;
        public Faculty Faculty;
        public Specialty Specialty;

        public override string ToString()
        {
            return String.Format("[{0} {1}, SSN {5}, student at {2} ({4} year in {3})]"
                , this.FirstName, this.LastName, this.University, this.Specialty, this.Course, this.SSN);
        }

        public override bool Equals(object obj)
        {
            if (Object.ReferenceEquals(this, obj))
            {
                return true;
            }

            Student other = obj as Student;

            if (obj == null)
            {
                return false;
            }

            return this.Equals(other);
        }

        public bool Equals(Student other)
        {
            if (Object.ReferenceEquals(this, other))
            {
                return true;
            }

            bool isEqual =
                this.FirstName == other.FirstName
                && this.MiddletName == other.MiddletName
                && this.LastName == other.LastName
                && this.SSN == other.SSN
                && this.University == other.University
                && this.Faculty == other.Faculty
                && this.Specialty == other.Specialty
                && this.MobilePhone == other.MobilePhone
                && this.Email == other.Email
                && this.Course == other.Course;

            return isEqual;
        }

        public override int GetHashCode()
        {
            return this.SSN.GetHashCode();
        }

        public static bool operator ==(Student student1, Student student2)
        {
            return student1.Equals(student2);
        }

        public static bool operator !=(Student student1, Student student2)
        {
            return !(student1 == student2);
        }

        public object Clone()
        {
            Student newStudent = new Student() 
            { 
                FirstName = this.FirstName,
                MiddletName = this.MiddletName,
                LastName = this.LastName,
                SSN = this.SSN,
                University = this.University,
                Faculty = this.Faculty,
                Specialty = this.Specialty,
                MobilePhone = this.MobilePhone,
                Email = this.Email,
                Course = this.Course
            };

            return newStudent;
        }

        public int CompareTo(Student other)
        {
            if (this.FirstName != null)
            {
                int firstNameComareResult = this.FirstName.CompareTo(other.FirstName);

                if (firstNameComareResult != 0)
                {
                    return firstNameComareResult;
                }
            }

            if (this.MiddletName != null)
            {
                int middleNameComareResult = this.MiddletName.CompareTo(other.MiddletName);

                if (middleNameComareResult != 0)
                {
                    return middleNameComareResult;
                }
            }

            if (this.LastName != null)
            {
                int lastNameComareResult = this.LastName.CompareTo(other.LastName);

                if (lastNameComareResult != 0)
                {
                    return lastNameComareResult;
                }
            }

            return this.SSN.CompareTo(other.SSN);
        }
    }
}
